
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class changeCompleteStatus
 */
@WebServlet("/changeCompleteStatus")
public class changeCompleteStatus extends HttpServlet{//修改学生的任务完成情况
    public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String UserID = request.getParameter("UserID");
        String InviteID = request.getParameter("InviteID");
        String taskID = request.getParameter("taskID");
        String num = request.getParameter("num");
        String taskContent = request.getParameter("taskContent").replaceAll("@N", "").replaceAll("@Y", "");
        String[] taskContents = taskContent.split(";");
        String taskCompleteStatu = null;//记录获取的题目完成状态
        String taskFormName = null;//记录变化的表单控件名称
        StringBuilder taskBuilder = new StringBuilder().append("");
        float completeYes = 0;//统计已完成的题目书数量
        float total = taskContents.length;//所有的题目数量
        for (int i = 0; i < taskContents.length; i++) {
            taskFormName = "task"+i;
            taskCompleteStatu = request.getParameter(taskFormName);
            if(taskCompleteStatu != null && taskCompleteStatu.equals("Y")) {
                taskBuilder.append(taskContents[i]+"@Y;");
                completeYes++;
            }
            if(taskCompleteStatu == null) {
                taskBuilder.append(taskContents[i]+"@N;");
            }
        }
        float myNewCompleteRate = completeYes/total;
        taskContent = taskBuilder.toString();//重新修改完完成状态后的任务内容
        String url = "jdbc:mysql://localhost:3306/listsystem";//与本机的3306端口建立连接，进入数据库listsystem
        String driverName = "com.mysql.jdbc.Driver";//驱动注册
        String userName = "root";
        String password = "";
        String sql = "update taskcontent_stu set TaskContent='"+taskContent+"',MyCompleteRate="+myNewCompleteRate+" where StudentID='"+UserID+"' and TaskID='"+taskID+"'";
        Connection conn = null;//用于建立与数据库的连接
        Statement sta = null;
        try{
            Class.forName(driverName);// jdbc4.0 后无需使用这句进行驱动注册操作
            conn = DriverManager.getConnection(url, userName, password);
            if(conn != null) {
                sta = conn.createStatement();
            }
            if(sta != null) {
                sta.execute(sql);
            }
        }catch(Exception e){
            System.out.println(e);
        }finally {
            try {
                if(sta != null)
                    sta.close();
                if(conn != null)
                    conn.close();
                sta = null;
                conn = null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        out.print("<html>");
        out.print("<head>");
        out.print("<title>提交成功</title>");
        out.print("</head>");



        out.print("<meta charset=\"utf-8\" />\n" +
                "\t\t<title>雨滴特效</title>\n" +
                "\t\t<style>\n" +
                "\t\t\tbody {\n" +
                "\t\t\t\tmargin: 0;\n" +
                "\t\t\t}\n" +
                "\t\t\t\n" +
                "\t\t\tcanvas {\n" +
                "\t\t\t\tbackground-color: black;\n" +
                "\t\t\t\tdisplay: block;\n" +
                "\t\t\t}\n" +
                "\t\t\t\n" +
                "\t\t\t#ff {\n" +
                "\t\t\t\tposition: absolute;\n" +
                "\t\t\t\ttop: 30%;\n" +
                "\t\t\t\tleft: 30%;\n" +
                "\t\t\t\tz-index: 1;\n" +
                "\t\t\t\ttext-align: center;\n" +
                "\t\t\t\tcolor: white;\n" +
                "\t\t\t\tfont: \"楷体\";\n" +
                "\t\t\t}\n" +
                "\t\t\t\n" +
                "\t\t\timg {\n" +
                "\t\t\t\tposition: absolute;\n" +
                "\t\t\t\ttop: 10%;\n" +
                "\t\t\t\tleft: 20%;\n" +
                "\t\t\t\ttransform: scale(0.6);\n" +
                "\t\t\t}\n" +
                "\t\t</style><body>" +
                "<img src=\"image/welcome.png\" />\n" +
                "\n" +
                "\t\t<canvas class='rain'>\n" +
                "\t\t\t\n" +
                "\t\t</canvas>");



//            out.print("<body>");
        out.print("<div id='ff'><form action=\"changeCompleteStatus\" method=\"get\">");



//        out.print("<body>");
        out.print("<center>");
        out.print("<h3>提交成功<h3>");
        out.print("<input type=\"button\" value=\"返回\" onclick=\"history.back(-1)\">");
        //out.print("<input type=\"button\" value=\"返回\" onclick=\"window.location.href='showTaskContent?UserID="+UserID+"&InviteID="+InviteID+"&taskID="+taskID+"&num="+num+"'\">");
        out.print("</center>");
        out.print("</form></div>");



        out.print("</body>\t<script>\n" +
                "\t\tvar canvas = document.querySelector('.rain');\n" +
                "\t\tvar ctx = canvas.getContext('2d')\n" +
                "\t\tvar aRain = [];\n" +
                "\n" +
                "\t\tvar w = window.innerWidth;\n" +
                "\t\tvar h = window.innerHeight;\n" +
                "\t\tcanvas.width = w;\n" +
                "\t\tcanvas.height = h;\n" +
                "\t\twindow.onresize = function() {\n" +
                "\t\t\tcanvas.width = window.innerWidth;\n" +
                "\t\t\tcanvas.height = window.innerHeight;\n" +
                "\t\t}\n" +
                "\n" +
                "\t\tfunction random(min, max) {\n" +
                "\t\t\treturn Math.random() * (max - min) + min;\n" +
                "\t\t}\n" +
                "\n" +
                "\t\tfunction Rain() {};\n" +
                "\t\tRain.prototype = {\n" +
                "\t\t\tinit: function() {\n" +
                "\t\t\t\tthis.x = random(0, w);\n" +
                "\t\t\t\tthis.y = 0;\n" +
                "\t\t\t\tthis.vy = random(4, 6);\n" +
                "\t\t\t\tthis.h = random(0.8 * h, 0.9 * h)\n" +
                "\t\t\t\tthis.r = 1;\n" +
                "\t\t\t\tthis.vr = 1;\n" +
                "\t\t\t\tthis.rr = random(60, 130);\n" +
                "\t\t\t\tthis.ready = true;\n" +
                "\t\t\t},\n" +
                "\t\t\tdraw: function() {\n" +
                "\t\t\t\tif(this.y < this.h && this.ready) {\n" +
                "\t\t\t\t\tctx.beginPath();\n" +
                "\t\t\t\t\tctx.fillStyle = '#31f7f7';\n" +
                "\t\t\t\t\tctx.fillRect(this.x, this.y, 4, 10);\n" +
                "\t\t\t\t} else {\n" +
                "\t\t\t\t\tctx.beginPath();\n" +
                "\t\t\t\t\tctx.strokeStyle = '#31f7f7';\n" +
                "\t\t\t\t\tctx.arc(this.x, this.y, this.r, 0, Math.PI * 2);\n" +
                "\t\t\t\t\tctx.stroke();\n" +
                "\t\t\t\t}\n" +
                "\t\t\t},\n" +
                "\t\t\tmove: function() {\n" +
                "\t\t\t\tif(!this.ready) {\n" +
                "\t\t\t\t\tif(this.r < 1) {\n" +
                "\t\t\t\t\t\tthis.ready = true;\n" +
                "\t\t\t\t\t} else {\n" +
                "\t\t\t\t\t\tthis.r--;\n" +
                "\t\t\t\t\t\tthis.draw();\n" +
                "\t\t\t\t\t\treturn;\n" +
                "\t\t\t\t\t}\n" +
                "\t\t\t\t}\n" +
                "\n" +
                "\t\t\t\tif(this.y < this.h) {\n" +
                "\t\t\t\t\tthis.y += this.vy;\n" +
                "\t\t\t\t} else {\n" +
                "\t\t\t\t\tthis.r += this.vr;\n" +
                "\t\t\t\t};\n" +
                "\t\t\t\tif(this.r < this.rr) {\n" +
                "\t\t\t\t\tthis.draw();\n" +
                "\t\t\t\t} else {\n" +
                "\t\t\t\t\tthis.init();\n" +
                "\t\t\t\t};\n" +
                "\t\t\t}\n" +
                "\t\t}\n" +
                "\n" +
                "\t\tsetInterval(function() {\n" +
                "\t\t\tctx.fillStyle = 'rgba(0,0,0,0.05)';\n" +
                "\t\t\tctx.fillRect(0, 0, w, h);\n" +
                "\t\t\tfor(var item of aRain) {\n" +
                "\t\t\t\titem.move();\n" +
                "\t\t\t}\n" +
                "\t\t}, 1000 / 60)\n" +
                "\n" +
                "\t\tfunction creatRain(num) {\n" +
                "\t\t\tfor(var i = 0; i < num; i++) {\n" +
                "\t\t\t\tsetTimeout(function() {\n" +
                "\t\t\t\t\tvar rain = new Rain();\n" +
                "\t\t\t\t\taRain.push(rain);\n" +
                "\t\t\t\t\train.init();\n" +
                "\t\t\t\t\train.draw();\n" +
                "\t\t\t\t}, 200 * i)\n" +
                "\t\t\t}\n" +
                "\t\t}\n" +
                "\n" +
                "\t\t//\t\tcreatRain(60);\n" +
                "\t\tcanvas.onmousedown = function(e) {\n" +
                "\t\t\tvar rain = new Rain();\n" +
                "\t\t\train.init();\n" +
                "\t\t\train.x = e.offsetX;\n" +
                "\t\t\train.y = e.offsetY;\n" +
                "\t\t\train.r = random(5, 30);\n" +
                "\t\t\train.ready = false;\n" +
                "\t\t\taRain.push(rain);\n" +
                "\t\t}</script></html>");
    }
}
